package com.ctg.payroll2.action.workflow;

import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.action.BaseAction;
import com.ctg.payroll2.bean.filter.workflow.FlowRoleFilter;
import com.ctg.payroll2.bean.po.workflow.FlowRole;
import com.ctg.payroll2.service.workflow.WorkflowRoleService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.core.security.Exception.RoleNameException;
import com.ctg.platform.core.security.Exception.SecurityException;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;

public class WorkflowRoleAction extends BaseAction {
	private static final long serialVersionUID = -6626107117458730354L;

	private WorkflowRoleService service;

	/**
	 * 查询调价过滤类
	 */
	private FlowRoleFilter filter;

	private FlowRole flowRole;

	/**
	 * 翻页结果
	 */
	private Page result;

	@Override
	protected void commonOperations() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void createEntity() {
		try {
			if (null == service.insertFlowRole(flowRole, currentUser)) {
				// System.out.println("插入失败");
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (RoleNameException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void deleteEntity() {
		try {
			// System.out.println(filter.getIds());
			service.deleteFlowRoleByIds(filter.getIds(), currentUser);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void exportXLS() {

	}

	@Override
	protected void queryEntity() {
		if (filter == null)
			filter = new FlowRoleFilter();
		filter.setStartIndex(Parse.toInteger(request
				.getParameter(Constants.START), 0));
		filter.setEndIndex(Parse.toInteger(request
				.getParameter(Constants.LIMIT), Constants.DEFAULT_PAGE_SIZE));
		try {
			result = service.queryFlowRoleByFilter(filter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void retrieveEntity() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void updateEntity() {
		// TODO Auto-generated method stub

	}

	/**
	 * @return 返回filter
	 */
	public FlowRoleFilter getFilter() {
		return filter;
	}

	/**
	 * @param filter
	 *            设置filter
	 */
	public void setFilter(FlowRoleFilter filter) {
		this.filter = filter;
	}

	/**
	 * @return 返回flowRole
	 */
	public FlowRole getFlowRole() {
		return flowRole;
	}

	/**
	 * @param flowRole
	 *            设置flowRole
	 */
	public void setFlowRole(FlowRole flowRole) {
		this.flowRole = flowRole;
	}

	/**
	 * @return 返回result
	 */
	public Page getResult() {
		return result;
	}

	/**
	 * @param result
	 *            设置result
	 */
	public void setResult(Page result) {
		this.result = result;
	}

	/**
	 * @param service
	 *            设置service
	 */
	public void setService(WorkflowRoleService service) {
		this.service = service;
	}

	HashMap<String, Object> paraMap = new HashMap<String, Object>();

	Page roelUser;

	public Page getRoelUser() {
		return roelUser;
	}

	public void setRoelUser(Page roelUser) {
		this.roelUser = roelUser;
	}

	/**
	 * 根据角色查询还未注册的账号 sami
	 * 
	 * @return
	 */
	public String queryRoleUserNotInRoleUser() {
		roelUser = null;
		paraMap.clear();
		HttpServletRequest request = ServletActionContext.getRequest();
		paraMap.put("flow_orgid", request.getParameter("flow_orgid"));
		if (!"".equals(request.getParameter("flow_username"))
				|| !"null".equals(request.getParameter("flow_username"))) {
			paraMap.put("flow_username", request.getParameter("flow_username"));
		}
		roelUser = service.queryRoleUserNotInRoleUser(paraMap, Parse.toInteger(
				request.getParameter("start"), 0), Parse.toInteger(request
				.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE));
		return SUCCESS;
	}

	/**
	 * 查询角色对应的账号 sami
	 * 
	 * @return
	 */
	public String queryFlowUserByRole() {
		try {
			roelUser = null;
			paraMap.clear();
			HttpServletRequest request = ServletActionContext.getRequest();
			paraMap.put("flow_roleid", request.getParameter("flow_roleid"));
			if (!"".equals(request.getParameter("flow_username"))
					|| !"null".equals(request.getParameter("flow_username"))) {
				paraMap.put("flow_username", request
						.getParameter("flow_username"));
			}
			roelUser = service.queryFlowUserByRole(paraMap, Parse.toInteger(
					request.getParameter("start"), 0), Parse.toInteger(request
					.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 新增角色账号 sami
	 * 
	 * @return
	 */
	public String insertRoleUser() {
		HttpServletRequest request = ServletActionContext.getRequest();

		String insertAccounts = request.getParameter("accountIds");
		
		String[] ids;
		ids = insertAccounts.split(",");
		for (int i = 0; i < ids.length; i++) {
			paraMap.clear();
			paraMap.put("flow_userid", ids[i]);
			if (!"".equals(request.getParameter("flow_roleid"))
					|| !"null".equals(request.getParameter("flow_roleid"))) {
				paraMap.put("flow_roleid", request.getParameter("flow_roleid"));
			}
			service.insertUserByRole(paraMap);
		}
		return SUCCESS;
	}

	/**
	 * 删除角色对应账号 sami
	 * 
	 * @return
	 */
	public String deleteRoleUser() {

		HttpServletRequest request = ServletActionContext.getRequest();

		String deleteAccounts = request.getParameter("deleteAccounts");
		String[] ids;
		ids = deleteAccounts.split(",");
		for (int i = 0; i < ids.length; i++) {
			paraMap.clear();
			paraMap.put("flow_userid", ids[i]);
			if (!"".equals(request.getParameter("flow_roleid"))
					|| !"null".equals(request.getParameter("flow_roleid"))) {
				paraMap.put("flow_roleid", request.getParameter("flow_roleid"));
			}
			service.deleteRoleUser(paraMap);
		}

		return SUCCESS;
	}
}
